<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of add_new
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Car extends MY_Controller {

    


    function Car() {
        parent :: __construct();
        $this->load->model('pickup_model');
        $this->load->model('location_model');
        $this->load->model('member_model');
        $this->load->model('management_model');
        $this->load->model('settings_model');
        $this->load->model('listing_model');
        
        $this->commoncall->check_member_login();
    }

    function add() {
       
        $id = intval($this->session->userdata('mem_id'));
        $data['member']['info'] = $this->member_model->get_member($id);
        $data['member']['listing'] = $this->member_model->get_member_listings($id);

        $data['car']['make'] = $this->pickup_model->get_car_makes();
        $data['car']['state'] = $this->location_model->get_states(config_item('default_country_code'));
        $data['car']['make_year'] = $this->pickup_model->populate_year('P', 15, date('Y', now()));
        $data['car']['car_owner'] = $this->pickup_model->get_pickup(9);
        $data['car']['state'] = $this->location_model->get_states(config_item('default_country_code'));
        $data['car']['car_tax'] = $this->pickup_model->get_pickup(10);
        $data['car']['car_insurance'] = $this->pickup_model->get_pickup(11);
        $data['car']['car_insurance_month'] = $this->pickup_model->get_pickup(12);
        $data['car']['car_insurance_year'] = $this->pickup_model->populate_year('N', 20, date('Y') - 5);

        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('footer', 'footer/footer')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->build('pages/member/addnew_car', $data);
    }

    function submit() {
     //print $this->input->post('car_comments');
        $this->form_validation->set_rules('car_make', 'Error! Car Make', 'trim|required');
        $this->form_validation->set_rules('car_model', 'Error! Car Model', 'trim|required');
        $this->form_validation->set_rules('car_version', 'Error! Car Version', 'trim|required');

        $this->form_validation->set_rules('car_make_year', 'Error! Make Year', 'trim|required');
        $this->form_validation->set_rules('car_owner', 'Error! Car Owner', 'trim|required');
        $this->form_validation->set_rules('car_color', 'Error! Car Color', 'trim|required');

        $this->form_validation->set_rules('car_km_done', 'Error! Kilomiter done', 'trim|required|numeric');
        $this->form_validation->set_rules('car_reg_no', 'Error! Registration No', 'trim|required');
        $this->form_validation->set_rules('car_reg_at', 'Error! Registerd at', 'trim|required');

        $this->form_validation->set_rules('state', 'Error! State', 'trim|required');
        $this->form_validation->set_rules('city', 'Error! City', 'trim|required');

        $this->form_validation->set_rules('car_insurance', 'Error! Car Insurance', 'trim|required');
        $this->form_validation->set_rules('car_insurance_year', 'Error! Insurance Valid upto (Year)', 'trim|required');
        $this->form_validation->set_rules('car_insurance_month', 'Error! Insurance Valid upto (Month)', 'trim|required');

        $this->form_validation->set_rules('car_tax', 'Error! Car Tax', 'trim|required');
        $this->form_validation->set_rules('car_price', 'Error! Expected Price', 'trim|required|numeric');

        $this->form_validation->set_rules('car_comments', 'Error! Comments', 'trim|required|min_length[50]');
        $this->form_validation->set_rules('car_agree', 'Error! Check Agreement', 'trim|required');

        
        
        if ($this->form_validation->run() == FALSE) {
            $msg = "Invalid Data entry, All Fields are mandatory ..Please check ..";
            $this->session->set_flashdata('message', $msg);
            if ($this->input->post('mode') == 'EDIT') {
                redirect('member/car/edit/' . $this->input->post('listing_id'));
            } else {
                redirect('member/car/add');
            }
        } else {
            $post_data = array(
                'type' => 'CAR',
                'user' => $this->session->userdata('mem_id'),
                'make' => $this->input->post('car_make'),
                'model' => $this->input->post('car_model'),
                'version' => $this->input->post('car_version'),
                'make_year' => $this->input->post('car_make_year'),
                'owner' => $this->input->post('car_owner'),
                'color' => $this->input->post('car_color'),
                'km_done' => $this->input->post('car_km_done'),
                'reg_no' => $this->input->post('car_reg_no'),
                'reg_at' => $this->input->post('car_reg_at'),
                'country' => config_item('default_country_code'),
                'state' => $this->input->post('state'),
                'city' => $this->input->post('city'),
                'insurance' => $this->input->post('car_insurance'),
                'insurance_year' => $this->input->post('car_insurance_year'),
                'insurance_month' => $this->input->post('car_insurance_month'),
                'tax' => $this->input->post('car_tax'),
                'price' => $this->input->post('car_price'),
                'comments' => $this->input->post('car_comments'),
                'agree' => $this->input->post('car_agree'),
                'posted_on' => date('Y-M-d h:m:s', now())
            );

            if ($this->input->post('mode') == 'EDIT') {
                $this->db->where('id', intval($this->input->post('listing_id')));
                $this->db->update('listing_master', $post_data);
                print "Hi";
                //redirect('member/myaccount');
            } else {
                $this->db->insert('listing_master', $post_data);
                redirect('member/myaccount');
            }
        };
    }

    function edit($listing_id) {
       
        if ($listing_id) {
            $listing_id = intval($listing_id);
            $id = intval($this->session->userdata('mem_id'));
            $data['member']['info'] = $this->member_model->get_member($id);
            $data['member']['listing'] = $this->member_model->get_member_listings($id);

            $data['car']['make'] = $this->pickup_model->get_car_makes();
            $data['car']['state'] = $this->location_model->get_states(config_item('default_country_code'));
            $data['car']['make_year'] = $this->pickup_model->populate_year('P', 15, date('Y', now()));
            $data['car']['car_owner'] = $this->pickup_model->get_pickup(9);
            $data['car']['state'] = $this->location_model->get_states(config_item('default_country_code'));
            $data['car']['car_tax'] = $this->pickup_model->get_pickup(10);
            $data['car']['car_insurance'] = $this->pickup_model->get_pickup(11);
            $data['car']['car_insurance_month'] = $this->pickup_model->get_pickup(12);
            $data['car']['car_insurance_year'] = $this->pickup_model->populate_year('N', 20, date('Y') - 5);
            //-Get Listing Information --//
            $data['listing']['info'] = $this->listing_model->get_listing($listing_id);
            
            $this->template->title('Welcome', 'carz.com')
                    ->set_partial('header', 'header/header')
                    ->set_partial('footer', 'footer/footer')
                    ->set_partial('customer_care', 'sidebar/customer_care')
                    ->set_partial('social_share', 'sidebar/social_share')
                    ->set_partial('search_reviews', 'sidebar/search_reviews')
                    ->set_partial('recent_news', 'sidebar/recent_news')
                    ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                    ->build('pages/member/edit_car', $data);
        }
    }

    function delete($listing_id) {
        if ($listing_id) {
            $this->load->model('terms_model');

            //--delete video
            $sql = "delete from terms where term='listing-video' and listing_id=$listing_id";
            $this->db->query($sql);
            //--delete listings
            $sql = "delete from listing_master where id=$listing_id";
            $this->db->query($sql);
            //--Delete photo
            $data['listing-photo'] = $this->terms_model->get_terms('listing-photo', $listing_id);
            foreach ($data['listing-photo'] as $photo) {
                $fname = "./assets/images/data/photo/" . $photo->description;
                if (file_exists($fname))
                    unlink($fname);
            }
            $sql = "delete from terms where term='listing-photo' and listing_id=$listing_id";
            $this->db->query($sql);

            redirect('member/myaccount');
        }
    }

//--POST REQUIREMENT --------//
//--Subha (01/09/2012) ---//
    function post_requirement() {
        $id = intval($this->session->userdata('mem_id'));
        $data['member']['info'] = $this->member_model->get_member($id);
        $data['member']['listing'] = $this->member_model->get_member_listings($id);

        $data['car']['make'] = $this->pickup_model->get_car_makes();
        $data['car']['state'] = $this->location_model->get_states(config_item('default_country_code'));
        $data['car']['make_year'] = $this->pickup_model->populate_year('P', 15, date('Y', now()));
        $data['car']['car_owner'] = $this->pickup_model->get_pickup(9);
        $data['car']['state'] = $this->location_model->get_states(config_item('default_country_code'));
        $data['car']['car_tax'] = $this->pickup_model->get_pickup(10);
        $data['car']['car_insurance'] = $this->pickup_model->get_pickup(11);
        $data['car']['car_insurance_month'] = $this->pickup_model->get_pickup(12);
        $data['car']['car_insurance_year'] = $this->pickup_model->populate_year('N', 20, date('Y') - 5);

        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('footer', 'footer/footer')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->build('pages/member/post_car_requirement', $data);
    }

//--Subha (01/09/2012) -------//    
    function post_requirement_submit() {
        $this->form_validation->set_rules('car_make', 'Error! Car Make', 'trim|required');
        $this->form_validation->set_rules('car_model', 'Error! Car Model', 'trim|required');
        $this->form_validation->set_rules('car_version', 'Error! Car Version', 'trim|required');

        $this->form_validation->set_rules('car_make_year', 'Error! Make Year', 'trim|required');
        $this->form_validation->set_rules('car_owner', 'Error! Car Owner', 'trim|required');
        $this->form_validation->set_rules('car_color', 'Error! Car Color', 'trim|required');
        $this->form_validation->set_rules('state', 'Error! State', 'trim|required');
        $this->form_validation->set_rules('city', 'Error! City', 'trim|required');
        $this->form_validation->set_rules('car_price', 'Error! Expected Price', 'trim|required|numeric');
        $this->form_validation->set_rules('car_comments', 'Error! Comments', 'trim|required|min_length[50]');
        $this->form_validation->set_rules('car_agree', 'Error! Check Agreement', 'trim|required');


        if ($this->form_validation->run() == FALSE) {
            $msg = "Invalid Data entry !";
            $this->session->set_flashdata('message', $msg);
            redirect('member/car/post_requirement');
        } else {
            $post_data = array(
                'type' => 'CAR',
                'user' => $this->session->userdata('mem_id'),
                'make' => $this->input->post('car_make'),
                'model' => $this->input->post('car_model'),
                'version' => $this->input->post('car_version'),
                'make_year' => $this->input->post('car_make_year'),
                'owner' => $this->input->post('car_owner'),
                'color' => $this->input->post('car_color'),
                'country' => config_item('default_country_code'),
                'state' => $this->input->post('state'),
                'city' => $this->input->post('city'),
                'price' => $this->input->post('car_price'),
                'comments' => $this->input->post('car_comments'),
                'agree' => $this->input->post('car_agree'),
                'posted_on' => date('Y-M-d', now())
            );
            $this->db->insert('post_requirement', $post_data);
            $msg = "Successfull ! Waiting for moderation..";
            $this->session->set_flashdata('message', $msg);
            redirect('member/car/post_requirement');
        }
    }

}

?>
